﻿using AutoMapper;
using log4net;
using System;
using System.Linq;
using WinService_DatabaseMaintain.db;
using WinService_DatabaseMaintain.Properties;

namespace WinService_DatabaseMaintain
{
    public class DbMovingBackup
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(DbMovingBackup));

        public static void BackupRealData()
        {
            try
            {
                Mapper.Initialize(x => x.CreateMap<data_real_time, data_real_time_history>());
                Mapper.Initialize(x => x.CreateMap<xaxis_realtime, xaxis_realtime_history>());


                var year = DateTime.Now.Year - Settings.Default.RetainYears + 1;
                var endData = new DateTime(year, 1, 1, 0, 0, 0);
                using (var ctx = new DatagatewayEntities())
                {
                    var mains = ctx.xaxis_realtime.Where(t => t.datatime < endData).OrderBy(t => t.datatime).Take(1000).ToList();
                    var mainsIds = mains.Select(t => t.id);
                    var subs = ctx.data_real_time.Where(t => mainsIds.Contains(t.ID)).ToList();

                    Log.Info("主记录数:"+ mains.Count+",从记录数:"+subs.Count);
                    foreach (var sub in subs)
                    {
                        var subHistory = Mapper.Map<data_real_time_history>(sub);
                        ctx.data_real_time_history.Add(subHistory);
                    }

                    foreach (var main in mains)
                    {
                        var mainHistory = Mapper.Map<xaxis_realtime_history>(main);
                        ctx.xaxis_realtime_history.Add(mainHistory);
                    }
                    foreach (var sub in subs)
                    {
                        ctx.data_real_time.Remove(sub);
                    }
                    foreach (var main in mains)
                    {
                        ctx.xaxis_realtime.Remove(main);
                    }

                    ctx.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
        public static void BackupMinuteData()
        {
            try
            {
                Mapper.Initialize(x => x.CreateMap<data_minute, data_minute_history>());
                Mapper.Initialize(x => x.CreateMap<xaxis_minute, xaxis_minute_history>());


                var year = DateTime.Now.Year - Settings.Default.RetainYears + 1;
                var endData = new DateTime(year, 1, 1, 0, 0, 0);
                using (var ctx = new DatagatewayEntities())
                {
                    var mains = ctx.xaxis_minute.Where(t => t.datatime < endData).OrderBy(t => t.datatime).Take(1000).ToList();
                    var mainsIds = mains.Select(t => t.id);
                    var subs = ctx.data_minute.Where(t => mainsIds.Contains(t.ID)).ToList();
                    Log.Info("主记录数:" + mains.Count + ",从记录数:" + subs.Count);

                    foreach (var sub in subs)
                    {
                        var subHistory = Mapper.Map<data_minute_history>(sub);
                        ctx.data_minute_history.Add(subHistory);
                    }

                    foreach (var main in mains)
                    {
                        var mainHistory = Mapper.Map<xaxis_minute_history>(main);
                        ctx.xaxis_minute_history.Add(mainHistory);
                    }
                    foreach (var sub in subs)
                    {
                        ctx.data_minute.Remove(sub);
                    }
                    foreach (var main in mains)
                    {
                        ctx.xaxis_minute.Remove(main);
                    }

                    ctx.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
        public static void BackupHourData()
        {
            try
            {
                Mapper.Initialize(x => x.CreateMap<data_hour, data_hour_history>());
                Mapper.Initialize(x => x.CreateMap<xaxis_hour, xaxis_hour_history>());


                var year = DateTime.Now.Year - Settings.Default.RetainYears + 1;
                var endData = new DateTime(year, 1, 1, 0, 0, 0);
                using (var ctx = new DatagatewayEntities())
                {
                    var mains = ctx.xaxis_hour.Where(t => t.datatime < endData).OrderBy(t => t.datatime).Take(1000).ToList();
                    var mainsIds = mains.Select(t => t.id);
                    var subs = ctx.data_hour.Where(t => mainsIds.Contains(t.ID)).ToList();
                    Log.Info("主记录数:" + mains.Count + ",从记录数:" + subs.Count);

                    foreach (var sub in subs)
                    {
                        var subHistory = Mapper.Map<data_hour_history>(sub);
                        ctx.data_hour_history.Add(subHistory);
                    }

                    foreach (var main in mains)
                    {
                        var mainHistory = Mapper.Map<xaxis_hour_history>(main);
                        ctx.xaxis_hour_history.Add(mainHistory);
                    }
                    foreach (var sub in subs)
                    {
                        ctx.data_hour.Remove(sub);
                    }
                    foreach (var main in mains)
                    {
                        ctx.xaxis_hour.Remove(main);
                    }

                    ctx.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }

    }
}
